package com.graduation.aspect;

import com.alibaba.fastjson.JSON;
import com.graduation.util.MD5Util;
import com.graduation.util.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.lang.reflect.Method;

@Aspect
@Component
@Slf4j
public class NoRepeatSubmitAspect {

    @Resource
    RedisUtil redisUtil;


    @Pointcut("execution(public * com.graduation.information.table.*.*Controller.*(..))")
    public void excudeService() {
    }

    @Before("excudeService()")
    public Object doAround(JoinPoint pjp) throws Exception {
        MethodSignature signature = (MethodSignature) pjp.getSignature();
        Method method = signature.getMethod();
        NoRepeatSubmit annotation = method.getAnnotation(NoRepeatSubmit.class);
        if (null != annotation) {
            Object[] args = pjp.getArgs();
            String params = MD5Util.MD5Encode(JSON.toJSONString(args),"");
            String rediskey = method.getName() + ":" + params;
            if (redisUtil.hasKey(rediskey)) {
                throw new Exception("重复提交");
            }
            redisUtil.set(method.getName() + ":" + params, "", 3);
        }
        return null;
    }

}
